package se.mbaeumer.fxtodo.core.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Calendar;
import java.util.List;

public class DateUtils {
	/**
	 * Gets the start and end of the current week
	 * 
	 * @param from
	 *            the start of the week
	 * @param to
	 *            the end of the week
	 */
	public static List<Date> getWeekFromTo(Date from, Date to) {
		List<Date> dates = new ArrayList<Date>();
		Calendar c = Calendar.getInstance();
		Date current = new Date();
		c.setTime(current);
		int currentDay = c.get(Calendar.DAY_OF_WEEK);
		int firstDay = c.getFirstDayOfWeek();
		c.add(Calendar.DATE, (currentDay - firstDay) * -1);
		from = c.getTime();
		dates.add(from);
		c.add(Calendar.DATE, Calendar.DAY_OF_WEEK);
		to = c.getTime();
		dates.add(to);
		return dates;
	}

	/**
	 * Gets the start and end of the current month
	 * 
	 * @param from
	 *            the start
	 * @param to
	 *            the end
	 */
	public static List<Date> getMonthFromTo(Date from, Date to) {
		List<Date> dates = new ArrayList<Date>();
		Calendar c = Calendar.getInstance();
		Date current = new Date();
		from = new Date();
		to = new Date();
		c.setTime(current);
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH);
		c.set(year, month, 1);
		from = c.getTime();
		dates.add(from);
		while (c.get(Calendar.MONTH) == month) {
			c.add(Calendar.DATE, 1);
		}
		to = c.getTime();
		dates.add(to);
		return dates;
	}

	/**
	 * Gets the current day
	 * 
	 * @param from
	 * @param to
	 */
	public static List<Date> getDayFromTo(Date from, Date to, boolean isTomorrow) {
		List<Date> dates = new ArrayList<Date>();
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		// c.add(Calendar.DATE, -1);
		
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND , 0);
		from = c.getTime();
		
		if (isTomorrow) {			
			c.add(Calendar.DATE , 1);
			from = c.getTime();
			dates.add(from);
			c.set(Calendar.HOUR_OF_DAY, 23);
			c.set(Calendar.MINUTE, 59);
			c.set(Calendar.SECOND , 59);
			to = c.getTime();
		}else{
			dates.add(from);
			c.set(Calendar.HOUR_OF_DAY, 23);
			c.set(Calendar.MINUTE, 59);
			c.set(Calendar.SECOND , 59);
			to = c.getTime();
		}
		dates.add(to);
		return dates;
	}

	/**
	 * Formats a date to match yyyy/MM/dd
	 * 
	 * @param date
	 * @return
	 */
	public static String formatDate(Date date) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return df.format(date);
	}
}
